home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Random2.0 / Source / TestPercent.m < prev   
Text File  |  1995-06-12  |  3KB  |  158 lines

  1. //
  2. // TestPercent.m
  3. //
  4. // This program tests the Random classes' abilities to generate
  5. // random percentages.
  6. //
  7.  
  8.  
  9. #import "Random.h"
  10. #import "StandardEngine.h"
  11. #import "ElkinsEngine.h"
  12. #import "R250Engine.h"
  13. #import <math.h>
  14. #import <stdio.h>
  15. #import <stdlib.h>
  16.  
  17.  
  18. #define DEFAULT_N    10000
  19. #define DEFAULT_M    20
  20.  
  21.  
  22. #define MAX_BAR        40
  23.  
  24.  
  25. //
  26. // test_percent()
  27. //
  28. // This function takes the id of a prepared Random
  29. // instance and prints out the percent_test
  30. // histogram using it.
  31. //
  32.  
  33. int test_percent(id myRand, int m, int n)
  34. {
  35.     int        max;                // Size of largest bin.
  36.     int        total;
  37.     int        i;                // Generic loop variable.
  38.     int        j;                // Generic loop variable.
  39.     double    x;                // The random number.
  40.     int        slot;                // The sorted position of a number.
  41.     int        barsize;            // The length of a result bar.
  42.     int        *slots;                // The bin array (dynamically allocated).
  43.     
  44.     //
  45.     // Allocate and initialize slot array:
  46.     //
  47.     
  48. //    printf("Initializing slot array...\n");
  49.     
  50.     slots = (int *)malloc(m * sizeof(int));
  51.     for(i = 0; i < m; i++)
  52.         slots[i] = 0;
  53.     
  54.     //
  55.     // Make n random numbers:
  56.     //
  57.     
  58. //    printf("Making %d random numbers...\n", n);
  59.     
  60.     max = 0;
  61.     for(i = 0; i < n; i++) {
  62.         x = [myRand percent];
  63.     
  64.     slot = (int)floor(x * m);
  65.     if((slot >= 0) && (slot < m)) {        // If it is in range,
  66.         if((++slots[slot]) > max) {        //   count it.
  67.         max = slots[slot];
  68.         }
  69.     }
  70.     else {
  71.         printf(">> Value %f out of range!\n", x);
  72.     }
  73.     }
  74.     
  75.     //
  76.     // Print the results:
  77.     //
  78.     
  79. //    printf("Printing the results...\n");
  80.     
  81.     total = 0;
  82.     for(i = 0; i < m; i++) {
  83.         printf("%1.6f - %1.6f: %6d:", i * (1.0 / m), (i + 1) * (1.0 / m), slots[i]);
  84.     
  85.     total += slots[i];
  86.     barsize = (int)((double)slots[i] / ((double)max / (double)MAX_BAR));
  87.     for(j = 0; j < barsize; j++)
  88.         printf("*");
  89.     printf("\n");
  90.     }
  91.     
  92.     printf("Max =   %6d\n", max);
  93.     printf("Total = %6d\n", total);
  94.     
  95.     //
  96.     // Clean up:
  97.     //
  98.     
  99.     free(slots);
  100.     
  101.     //
  102.     // Return to caller:
  103.     //
  104.     
  105.     return 0;
  106. }
  107.  
  108.  
  109. //
  110. // main()
  111. //
  112.  
  113. int main(int argc, char *argv[])
  114. {
  115.     id        myRand;                // Random number generator.
  116.     int        n;                // Quantity of numbers to generate.
  117.     int        m;                // Number of slots in which to sort them.
  118.     
  119.     switch(argc) {
  120.         case 2:
  121.         sscanf(argv[1], "%d", &n);;
  122.         m = DEFAULT_M;
  123.         break;
  124.     case 3:
  125.         sscanf(argv[1], "%d", &n);;
  126.         sscanf(argv[2], "%d", &m);;
  127.         break;
  128.     default:
  129.         n = DEFAULT_N;
  130.         m = DEFAULT_M;
  131.         break;
  132.     }
  133.     
  134.     printf("TestPercent: Testing StandardEngine class:\n");
  135.     myRand = [[Random alloc] initEngineClass:[StandardEngine class]];
  136.     test_percent(myRand, m, n);
  137.     printf("\n\n");
  138.     [myRand free];
  139.     
  140.     printf("TestPercent: Testing ElkinsEngine class:\n");
  141.     myRand = [[Random alloc] initEngineClass:[ElkinsEngine class]];
  142.     test_percent(myRand, m, n);
  143.     printf("\n\n");
  144.     [myRand free];
  145.     
  146.     printf("TestPercent: Testing R250Engine class:\n");
  147.     myRand = [[Random alloc] initEngineClass:[R250Engine class]];
  148.     test_percent(myRand, m, n);
  149.     printf("\n\n");
  150.     [myRand free];
  151.     
  152.     return 0;
  153. }
  154.  
  155.  
  156. //
  157. // End of file.
  158. //